
from sqlalchemy import Column, Integer, String, Sequence, Index, Date, DateTime
from sqlalchemy.types import Binary, LargeBinary
from sqlalchemy import func, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, remote, foreign
from sqlalchemy_utils import LtreeType, Ltree
from flask_sqlalchemy import SQLAlchemy

import sys
import datetime

Base = declarative_base()

class Node(Base):
    __tablename__ = 'nodes'
    path = Column(LtreeType, nullable=False)
    __table_args__ = (Index('ix_nodes_path', path, postgresql_using="gist"),)
    repository_id  = Column(String(20), nullable=False, primary_key=True)
    repositoryname = Column(String(254), unique=True)

    def __str__(self):
        return self.name
    def __repr__(self):
        return 'Node({})'.format(self.name)

        
class Files(Base):
    __tablename__ = 'files'
    filepath =      Column(LtreeType, nullable=False)
    __table_args__ = (Index('ix_nodes_path', filepath, postgresql_using="gist"),)
    repository_id = Column(String(20), nullable=False, primary_key=True)
    treehash =      Column(String(40), nullable=False, primary_key=True)
    filename =      Column(String(254),unique=True, nullable=False)
    blobhash =      Column(String(40), nullable=False)
    blob_content =  Column(LargeBinary, default=b'\x08')
    tree_entry =    Column(String(16), nullable=False)
    blob_size =     Column(Integer)

    def __str__(self):
        return self.name
    def __repr__(self):
        return 'Files({})'.format(self.name)

    
class CommitFiles(Base):
    __tablename__ = 'commitfiles'
    filepath  =     Column(LtreeType, nullable=False)
    __table_args__ = (Index('ix_nodes_path', filepath, postgresql_using="gist"),)
    repository_id = Column(String(20), nullable=False, primary_key=True)
    commithash =    Column(String(40), nullable=False, primary_key=True)
    treehash =      Column(String(40), nullable=False)
    blobhash =      Column(String(40), nullable=False)

    def __str__(self):
        return self.name
    def __repr__(self):
        return 'CommitFiles({})'.format(self.name)
 
        
class Commits(Base):
    __tablename__ = 'commits'
    repository_id =  Column(String(20), nullable=False)
    treehash =       Column(String(40), nullable=False, primary_key=True)
    commithash =     Column(String(40), nullable=False)   
    commitauthor_name  = Column(String(45), nullable=False)
    commitauthor_email = Column(String(254))
    commitauthor_time  = Column(DateTime(),)
    committerx_name    = Column(String(45), nullable=False)
    committerx_email   = Column(String(254))
    committerx_time    = Column(DateTime(),)
    commit_message     = Column(String(2048))

def __str__(self):
        return self.name
    def __repr__(self):
        return 'Commits({})'.format(self.name)

 

class TreeRecords(Base):
    __tablename__ = 'treerecords'
    repository_id =   Column(String(20), nullable=False, primary_key=True)
    treehash =        Column(String(40), nullable=False, primary_key=True)
    blobhash =        Column(String(40), nullable=False)
    tree_entry_mode = Column(String(16))
    tree_entry_name = Column(String(254), nullable=False)
   
    def __str__(self):
        return self.name
    def __repr__(self):
        return 'TreeRecords({})'.format(self.name)


class Refs(Base):
    __tablename__ = 'refs'
    repository_id  = Column(String(20), nullable=False, primary_key=True)
    commithash     = Column(String(40), nullable=False, primary_key=True)
    refname        = Column(String(254), nullable=False, unique=True)
    
    def __str__(self):
        return self.name
    def __repr__(self):
        return 'Refs({})'.format(self.name)


class Remotes (Base):
    __tablename__ = 'remotes'
    rid =                 Column(Integer, primary_key=True)     
    repository_id  =      Column(String(20), nullable=False, primary_key=True)
    remotepush_url  =     Column(String(254), nullable=False)
    remotefetch_url =     Column(String(254), nullable=False)
    remotepush_refspec  = Column(String(254), nullable=False)
    remotefetch_refspec = Column(String(254), nullable=False)	
    remotehostname =      Column(String(96), nullable=False)
  
    def __str__(self):
        return self.name
    def __repr__(self):
        return 'Remotes({})'.format(self.name)

   
class RefCommits(Base):
    __tablename__ = 'refcommits'
    repository_id  = Column(String(20), nullable=False, primary_key=True)
    commithash     = Column(String(40), nullable=False, primary_key=True)
    refname        = Column(String(45), nullable=False)
    hindex         = Column(Integer)

   def __str__(self):
        return self.name
    def __repr__(self):
        return 'RefCommits({})'.format(self.name)

   
class CommitTrees(Base):
    __tablename__ = 'committrees'
    repository_id =  Column(String(20), nullable=False, primary_key=True)
    commithash =     Column(String(40), nullable=False, primary_key=True)
    treehash =       Column(String(40), nullable=False)

   def __str__(self):
        return self.name
    def __repr__(self):
        return 'CommitTrees({})'.format(self.name)
    

class CommitBlobs(Base):
    __tablename__ = 'commitblobs'
    repository_id =  Column(String(20), nullable=False, primary_key=True)
    commithash =     Column(String(40), nullable=False, primary_key=True)
    blobhash =       Column(String(40), nullable=False)
   
   def __str__(self):
        return self.name
    def __repr__(self):
        return 'CommitBlobs({})'.format(self.name)


